package cn.csc.module.order.mq.producer;

import cn.csc.framework.mq.core.bus.AbstractBusProducer;
import cn.csc.module.order.dal.dataobject.item.OrderItemDO;
import cn.csc.module.order.message.OrderReceiptMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 买家确认收货消息
 * topic: order_receipt
 * @author liuchuang
 * @version 1.0
 * @date 2024/7/30 9:42
 * @modify
 */
@Slf4j
@Component
public class OrderReceiptProducer extends AbstractBusProducer {

    @Resource
    private StreamBridge streamBridge;

    /**
     * 发送 {@link OrderReceiptMessage} 消息
     *
     * @param list 订单子项列表
     */
    public Boolean sendOrderReceiptMessage(List<OrderItemDO> list) {
        log.info("sendOrderReceiptMessage订单确认收货发送消息：{}",list);
        List<BigInteger> orderItemIds= list.stream().map(o->o.getId()).collect(Collectors.toList());
        OrderReceiptMessage ReceiptMessage= new OrderReceiptMessage(orderItemIds);

        Message<OrderReceiptMessage> message = MessageBuilder.withPayload(ReceiptMessage)
                // 设置ORIGIN_MESSAGE_ID
                .setHeader(MessageConst.PROPERTY_ORIGIN_MESSAGE_ID, list.get(0).getOrderId())
                .build();
        return streamBridge.send("orderReceipt-out-0", message);
    }


}
